package com.ricervcvcer.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricervcvcer.common.util.ResultEntity;
import com.ricervcvcer.user.entity.SysUser;
import com.ricervcvcer.user.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author moge
 * @since 2022-01-10
 */
@RestController
@RequestMapping("user")
public class SysUserController {

    @Autowired
    ISysUserService sysUserService;

    @ApiOperation("查询所有用户，根据用户名称查询")
    @PostMapping("getUsers")
    public ResponseEntity getUsers(
            @ApiParam("页码") @RequestParam Integer page,
            @ApiParam("每页条数") @RequestParam Integer rows,
            @ApiParam("用户名") @RequestParam String uName){
        Page<SysUser> pages = sysUserService.getUsers(uName,page,rows);
        return ResponseEntity.ok(pages);
    }

    @ApiOperation("修改一个用户")
    @PostMapping("updateUser")
    public ResultEntity updateUser(
            @ApiParam("用户信息") @RequestBody SysUser user){
        boolean b = sysUserService.update(user);
        return ResultEntity.successWithoutData();
    }

    @PostMapping("isExits")
    @ApiOperation("判断用户是否存在，存在不添加 ，不存在添加")
    public ResultEntity isExits( @ApiParam("用户信息") @RequestBody SysUser user){
        boolean exits = sysUserService.isExits(user);
        return ResultEntity.successWithoutData();
    }
    @PostMapping("id/{id}")
    @ApiOperation("根据用户id查询用户信息")
    public ResultEntity<SysUser> id( @ApiParam("用户信息") @PathVariable Long id){
        SysUser user = sysUserService.getById(id);
        user.setUsrPassword(null);
        return ResultEntity.successWithData(user);
    }
}
